// pages/index/mindAnwser/mindReply/mindReply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    qid: '',
    question: '',
    show: false,
    content: '',
    replyList: [],
    fid: '',
    uname: '',
    user: ''
  },
  //监听input 输入
  change(e){
    const value = e.detail.value; 
    this.setData({
      content: value
    })
  },
  // 展示弹框
  show(e){
    this.setData({
      show: !this.data.show,
      content: '',
      fid: e.currentTarget.dataset.rid,
      uname: e.currentTarget.dataset.uname,
    });
  },
  //关闭弹窗
  onClose(){
    this.setData({
      show: !this.data.show
    })
  },
  //添加
  add(){
    let that = this;
    if(that.data.content==''){
      that.setData({
        show: !that.data.show,
        replyText: '',
      })
    }else{
      console.log(that.data.user.id);
      wx.request({
        url: that.data.url+'reply/add',
        method: 'POST',
        data: {
          userId: that.data.user.id,
          questionId: that.data.qid,
          replyText: that.data.content,
          fid: that.data.fid,
        },
        success(resp){
          that.setData({
            show: !that.data.show
          }),
          that.getReply();
        }
      })
    }
    
  },
  // 查找相关回复 根据qid查询该问题下的所有回复
  getReply(){
    let that = this;
    wx.request({
      url: that.data.url+'reply/findByQid',
      method: 'GET',
      data: {
        qid: that.data.qid,
      },
      success(resp){
        let result = resp.data;
        // console.log(result.data);
        that.setData({
          replyList: result.data,
        })
        console.log(that.data.replyList);
      }
    })
  },

  goIndex(){
    wx.navigateTo({
      url: '/pages/index/mindAnwser/mindAnswer',
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      url: app.globalData.URL,
      qid: options.qid,
      user: wx.getStorageSync('user')
    })
    console.log(this.data.user);
    let that = this;
    wx.request({
      url: that.data.url+'question/findById',
      method: 'GET',
      data: {
        id: that.data.qid
      },
      success(resp){
        let result = resp.data;
        // console.log(resp.data)
        that.setData({
          question: result.data
        }),
        that.getReply();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})